package com.pw.demo3.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pw.demo3.annotation.PerAnnotion;
import com.pw.demo3.bean.Department;
import com.pw.demo3.bean.Employee;
import com.pw.demo3.service.EmployeeService;
import com.pw.demo3.service.impl.DepartmentServiceImpl;
import com.pw.demo3.service.impl.EmployeeServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/department")
public class departmentController {

    @Autowired
    DepartmentServiceImpl departmentService;
    @Autowired
    EmployeeServiceImpl employeeService;

    @RequestMapping("/list")
    @PerAnnotion(name = "部门列表", expression = "department:list")
    public String departmentList(Model model,
                                 String currentPage) {
        int pageNum = 1;
        if (currentPage != null) {
            pageNum = Integer.parseInt(currentPage);
        }
        PageHelper.startPage(pageNum, 5);
        List<Department> departmentList = departmentService.selectAll();
        PageInfo<Department> pageInfo = new PageInfo<>(departmentList);

        Object a = SecurityUtils.getSubject().getPrincipal();
        Employee currentEmployee=employeeService.selectByName(a.toString());
        Session session=SecurityUtils.getSubject().getSession();
        session.setAttribute("employee",currentEmployee);

        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("msg", "消息");
        return "views/department/list";
    }

    @RequestMapping("/input")
    @PerAnnotion(name = "部门新增或者编辑操作", expression = "department:input")
    public String departmentInput(Model model,
                                  @RequestParam(name = "id", defaultValue = "0") String id) {
        if (!id.equals("0")) {
            Department department = departmentService.selectByPrimaryKey(Long.parseLong(id));
            model.addAttribute("department", department);
        }
        return "views/department/input";
    }

    @RequestMapping("/insert")
    @PerAnnotion(name = "权限测试", expression = "dep:permission")
    public String insert(HttpServletRequest request) {
        if (request.getParameter("id") != null) {
            update(request);
        } else {
            add(request);
        }
        return "redirect:/department/list";
    }

    public void add(HttpServletRequest request) {
        if (request.getParameter("name") != null && request.getParameter("sn") != null) {
            Department department = new Department();
            department.setName(request.getParameter("name"));
            department.setSn(request.getParameter("sn"));
            try {
                departmentService.insert(department);
            } catch (Exception e) {
                System.out.println("出错");
                e.printStackTrace();
            }
        }
    }

//    @RequestMapping("/toUpdate/{id}")
//    @PerAnnotion(name = "部门新增或者编辑操作", expression="department:input")
//    public String toUpdate(Model model,
//                           @PathVariable String id) {
//        Department department = departmentService.selectByPrimaryKey(Long.parseLong(id));
//        model.addAttribute("department", department);
//        return "views/department/input";
//    }

    //    @RequestMapping("/update")
    public String update(HttpServletRequest request) {
        Department department = departmentService.selectByPrimaryKey(Long.valueOf(request.getParameter("id")));
        if (request.getParameter("name") != null && request.getParameter("sn") != null) {
            department.setName(request.getParameter("name"));
            department.setSn(request.getParameter("sn"));
            try {
                departmentService.updateByPrimaryKey(department);
            } catch (Exception e) {
                System.out.println("错误");
                e.printStackTrace();
            }
        }

        return "redirect:/department/list";
    }

    @RequestMapping("/delete/{id}")
    @ResponseBody
    @PerAnnotion(name = "部门删除", expression = "department:delete")
    public Map<String, Object> delete(@PathVariable String id) {
        Map<String, Object> map = new HashMap<>();
        try {
            departmentService.deleteByPrimaryKey(Long.valueOf(id));
            map.put("success", true);
        } catch (Exception e) {
            map.put("success", false);
            map.put("msg", "错误");
            e.printStackTrace();
        }
        return map;
    }
}
